﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using System.Windows.Media.Animation;

namespace D3crft.Windows {
    /// <summary>
    /// Interaktionslogik für wLoadProgress.xaml
    /// </summary>
    public partial class wLoadProgress : Window {

        DispatcherTimer oTimer;

        List<string> slQueue_PrimaryInfo;
        List<string> slQueue_SecondaryInfo;
        List<int> ilQueue_MinTimeMS;
        List<float> flQueue_Percent;
        List<bool> blQueue_Finalizer;

        int setting_FinalizeTimeMS = 50;

        bool flag_FinalizationActive = false;
        bool flag_MinTimeDone = true;

        // ####################################################
        // #####    Object Initialization
        // ####################################################
        #region Object Initialization

        public wLoadProgress() {
            do_Initialize();
            do_Preset();
            do_Startup();
        }

        ~wLoadProgress() {
            do_Finalize();
        }

        private void do_Initialize() {
            InitializeComponent();
            oTimer = new DispatcherTimer();
            slQueue_PrimaryInfo = new List<string>();
            slQueue_SecondaryInfo = new List<string>();
            ilQueue_MinTimeMS = new List<int>();
            flQueue_Percent = new List<float>();
            blQueue_Finalizer = new List<bool>();
        }

        private void do_Preset() {
            oTimer.Tick += new EventHandler(ev_oTimer_Tick);
        }

        private void do_Startup() {
            Duration duration = new Duration(TimeSpan.FromMilliseconds(500));
            DoubleAnimation doubleanimation = new DoubleAnimation(((ProgressBar)FindName("pbProgress")).Value + 10, duration);
            ((ProgressBar)FindName("pbProgress")).BeginAnimation(ProgressBar.ValueProperty, doubleanimation);
        }

        private void do_Finalize() {

        }

        #endregion

        // ####################################################
        // #####    Public Access
        // ####################################################
        #region Public Access

        public void AddProgressInfo(string sPrimaryInfo, string sSecondaryInfo, int iMinTimeMS, float fPercent) {
            if (flag_FinalizationActive) return;

            if (flag_MinTimeDone) {
                ((Label)FindName("lblPrimaryInfo")).Content = sPrimaryInfo;
                ((Label)FindName("lblSecondaryInfo")).Content = sSecondaryInfo;
                ((ProgressBar)FindName("pbProgress")).Value = (double)fPercent;
                Duration duration = new Duration(TimeSpan.FromMilliseconds(500));
                DoubleAnimation doubleanimation = new DoubleAnimation(((ProgressBar)FindName("pbProgress")).Value + 10, duration);
                ((ProgressBar)FindName("pbProgress")).BeginAnimation(ProgressBar.ValueProperty, doubleanimation);
                flag_MinTimeDone = false;
                oTimer.Interval = TimeSpan.FromMilliseconds(iMinTimeMS);
                oTimer.Start();
            }
            else {
                slQueue_PrimaryInfo.Add(sPrimaryInfo);
                slQueue_SecondaryInfo.Add(sSecondaryInfo);
                ilQueue_MinTimeMS.Add(iMinTimeMS);
                flQueue_Percent.Add(fPercent);
                blQueue_Finalizer.Add(false);
            }
        }

        public void AddFinalization() {
            if (flag_FinalizationActive) return;

            if (flag_MinTimeDone) {
                ((ProgressBar)FindName("pbProgress")).Value = 100.0;
                oTimer.Interval = TimeSpan.FromMilliseconds(setting_FinalizeTimeMS);
                flag_FinalizationActive = true;
            }
            else {
                slQueue_PrimaryInfo.Add("Finalization");
                slQueue_SecondaryInfo.Add("");
                ilQueue_MinTimeMS.Add(setting_FinalizeTimeMS);
                flQueue_Percent.Add(100.0f);
                blQueue_Finalizer.Add(true);
            }
        }

        #endregion

        // ####################################################
        // #####    Private Functions
        // ####################################################
        #region Private Functions

        #endregion

        // ####################################################
        // #####    Events
        // ####################################################
        #region Events

        private void ev_oTimer_Tick(object sender, EventArgs ea) {
            oTimer.Stop();

            if (flag_FinalizationActive) {
                this.Close();
            }

            if (slQueue_PrimaryInfo.Count > 0) {
                if (blQueue_Finalizer[0]) {
                    flag_FinalizationActive = true;
                    ((ProgressBar)FindName("pbProgress")).Value = 100.0;
                    oTimer.Interval = TimeSpan.FromMilliseconds(setting_FinalizeTimeMS);
                    oTimer.Start();
                }
                else {
                    ((Label)FindName("lblPrimaryInfo")).Content = slQueue_PrimaryInfo[0];
                    ((Label)FindName("lblSecondaryInfo")).Content = slQueue_SecondaryInfo[0];
                    ((ProgressBar)FindName("pbProgress")).Value = (double)flQueue_Percent[0];
                    Duration duration = new Duration(TimeSpan.FromMilliseconds(500));
                    DoubleAnimation doubleanimation = new DoubleAnimation(((ProgressBar)FindName("pbProgress")).Value + 10, duration);
                    this.BeginAnimation(ProgressBar.ValueProperty, doubleanimation);
                    oTimer.Start();
                    slQueue_PrimaryInfo.RemoveAt(0);
                    slQueue_SecondaryInfo.RemoveAt(0);
                    ilQueue_MinTimeMS.RemoveAt(0);
                    flQueue_Percent.RemoveAt(0);
                    blQueue_Finalizer.RemoveAt(0);
                }

            }
            else {
                flag_MinTimeDone = true;
            }
        }

        #endregion
    }
}
